var base = require('/base_info.js');
var utils = require('/utils/index.js');
// 富文本解析
var wxParse = require('/pages/wxParse-master/wxParse.js');
Page({
  data: {
    imgUrl: base.imgUrl
  },
  // 跳转到协议卡详情页面
  agreementDetail (e) {
    my.navigateTo({
      url: './../agreement/agreement?id=' + e.currentTarget.dataset.id
    });
  },
  // 支付开通
  pay () {
    let that = this;
    // 请求订单号
    utils.getData('SingleAddCardOrder', {
      user_id: my.getStorageSync({
        key: 'uid', // 缓存数据的key
      }).data,
      equitycard_id: that.data.equityList.id,
      price: that.data.equityList.cost,
      buyer_id: my.getStorageSync({
        key: 'user_id', // 缓存数据的key 
      }).data,
      equitycard_name: that.data.equityList.name
    }, (res) => {
      let order_id = res.data.data.order_id;
      // 调起支付
      my.tradePay({
        tradeNO: res.data.data.trade_no,
        success: (res) => {
          if (res.resultCode == 9000) {
            my.redirectTo({
              url: './apply-success?id=' + order_id
            });
          } else {
            my.alert({
              content: '支付失败'
            });
          }
        },
        fail: () => {
          my.alert({
            content: '支付失败'
          });
        }
      });
    }, (err) => {
      console.log(err);
    });
  },
  // 获取权益卡列表
  getData () {
    // 获取权益卡列表
    let that = this;
    utils.getData('SingleEquityCardArticle', {
      // seller_id: base.seller_id
    }, (res) => {
      my.hideLoading();
      console.log(res.data.data);
      if (res.data.data == '') {
        that.setData({
          equityList: []
        })
        return false;
      };
      that.setData({
        equityList: res.data.data
      })
      wxParse.wxParse('introduce', 'html', res.data.data.introduce, that, 5);
      wxParse.wxParse('privilege', 'html', res.data.data.privilege, that, 5);
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  // 去使用
  goUse () {
    my.redirectTo({
      url: './../room/room-list'
    })
  },
  onLoad(query) {
    var that = this;
    var uid = my.getStorageSync({
      key: 'uid', // 缓存数据的key
    }).data;
    if (!uid || uid == "") {
      utils.getAuthCode(function(){});
      this.getData();
      return false;
    }
    this.getData();
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
